#copied from Final_Code_Replication_Final/Resubmission_Checks/P1_CloseToElection/Plot_04_Interactions.R

# Code for Figure 3: The Effect of Pretrial Incarceration on 2012 Turnout by Race-Income Groups

rm(list = ls())
## ---------------------------------------
## Load Packages 
## ---------------------------------------
require('dotwhisker')
require('gridExtra')
require('AER')
require('ivpack')
require('dplyr')
require('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812 <- constructIV(data0812)

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812 <- lastCase(data0812)

## ---------------------------------------
# Construct time variables
## ---------------------------------------
data0812$bail_year <- substring(data0812$bail_date, 1, 4)

data0812$bail_year[data0812$bail_year == 2008] <- 2009
data0812$court_timeQ <- NA
data0812$court_timeQ[data0812$court_time2 %in% c("1", "2", "3")] <- 1
data0812$court_timeQ[data0812$court_time2 %in% c("4", "5", "6")] <- 2
data0812$court_timeQ[data0812$court_time2 %in% c("7", "8", "9")] <- 3
data0812$court_timeQ[data0812$court_time2 %in% c("10", "11", "12")] <- 4

data0812$court_time_1D <- paste(data0812$bail_year, data0812$court_timeQ, sep = "-")

## ---------------------------------------
## Two Stage Least Squares Specifications
## ---------------------------------------
time.controls_m <- "as.factor(court_time_1D) * pti + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
time.controls_i <- "as.factor(court_time_1D) * judgeiv + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
case.controls <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
demo.controls <- "age_2012 + I(age_2012^2) + Female + as.factor(race) + vote2008 + as.factor(noteli08) + regis_before"
outc.1 <- "vote2012"
endo.1 <- "pti"
inst.1 <- "judgeiv"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls_m, "|", inst.1, "+", time.controls_i))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls_m, "+", demo.controls, "|", inst.1, "+", time.controls_i, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls_m, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls_i, "+", demo.controls, "+", case.controls))

## ---------------------------------------
## Plot Figure 4
## ---------------------------------------
m1a1 <- ivreg(form.1, data = data0812)
m1a2 <- ivreg(form.2, data = data0812)
m1a3 <- ivreg(form.3, data = data0812)

coefs <- coef(m1a3)
coefs <- round(coefs[grep("pti:as", names(coefs))], 3)
vars <- sqrt(diag(vcov(m1a3)))
moe <- vars[grep("pti:as", names(vars))] * qnorm(0.975)
coefs_up <- coefs + moe
coefs_do <- coefs - moe

pdf("../output/Figures/Figure_04.pdf", h=8,w=14)  
par(mar = c(8, 8, 2, 2), mgp=c(4,1,0))
plot(1:length(coefs), coefs, ylim = c(-0.22, 0.10), las = 1, pch = 16, xaxt = "n",
     ylab = "The Effect of Pretrial Incarceration on Turnout",
     xlab = "", cex = 1.5, cex.lab = 1.5, cex.axis = 1.5)
for(i in 1:length(coefs)) {
  lines(c(i, i), c(coefs_up[i], coefs_do[i]))
}
abline(h = 0, col = "red")
axis(1, at = 1:length(coefs), labels = c("2009-Q2",
                                         "2009-Q3",
                                         "2009-Q4",
                                         "2010-Q1",
                                         "2010-Q2",
                                         "2010-Q3",
                                         "2010-Q4",
                                         "2011-Q1",
                                         "2011-Q2",
                                         "2011-Q3",
                                         "2011-Q4",
                                         "2012-Q1",
                                         "2012-Q2",
                                         "2012-Q3",
                                         "2012-Q4"
), las = 2, cex.axis = 1.5)
dev.off()

cat("\nFigure 4: Dynamics of the Effect of Pretrial Incarceration on Turnout\n")
cat("Saved Figure 4 in /output/Figures/Figure_04.pdf\n\n")
